package com.goods.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.common.domain.GoodsVo;
import com.common.tool.WorkBlockingQueue;
import com.goods.dao.SearchGoodsDao;
import com.goods.lucene.tool.IndexManage;

/**
 * 定时任务 每天凌晨两点从数据库读取商品的增量
 * 
 * @author penghui.li
 * 
 */
@SuppressWarnings({ "unchecked", "rawtypes", "unused" })
@Service
public class SearchGoodsService {
	protected final Logger logger = LoggerFactory.getLogger(this.getClass()
			.getName());
	@Resource
	private SearchGoodsDao searchGoodsDao;

	@Resource
	private JdbcTemplate fclub_goods_db;

	public void process() {
		try {
			int count = searchGoodsDao.queryGoodsCount();
			int maxNum = 20000;// Integer.parseInt(ApplicationConfig.getApplicationConfig().getPropertie("search.goods.max.num",
								// "10000"));
			List<GoodsVo> list = new ArrayList<GoodsVo>();
			Map map = new HashMap();
			// 剩余
			int surplus = count;
			int startNo = 0;
			int endNo = maxNum - 1;
			int num = 0;
			while (surplus > 0) {
				map.put("startRow", startNo);
				if (surplus > maxNum) {
					map.put("endRow", endNo);
				} else {
					map.put("endRow", count - 1);
				}
				list.addAll(searchGoodsDao.queryGoods(map));
				System.out.println("-------startRow:" + map.get("startRow")
						+ "------endRow:" + map.get("endRow")
						+ "--------count:" + count);
				startNo += maxNum;
				endNo += maxNum;
				surplus -= maxNum;
				if (list.size() >= 20000) {
					IndexManage.create(list);
					list = new ArrayList();
				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public static void main(String[] args) {
		new SearchGoodsService().process();
		System.out.println(15001 / 10000);
	}
}
